package com.sunbeam.common.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sunbeam.common.consts.EConstants;

/**
 * 
 * @ClassName: StringUtils
 * @Description: string format tools
 * @author AromaSunbeam lyzpoq_gmail_com
 * @date 2013/07/22 14:12:51
 */
public class StringUtils {

	public static final String Empty = "";

	// *******************************TestMain**************************************
	/**
	 * 
	 * @Title: main
	 * @Description: TODO
	 * @param @param args
	 * @return void
	 * @throws
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// System.out.println(reverse("zxcvbnm"));
		// System.out.println(isNumber("13646654898546543.14548978549795"));
		System.out.println(subStrByByteLength("Shift-JIS1","aaa測a測試",9));
		// System.out.println(MathUtils.GetIndices(10, 1000)+1);
		// System.out.println(getStringSequence(1, 4));
		// for(int i=0;i<10;i++){
		// System.out.println(getStringSequence(i + 1, MathUtils.GetIndices(10,
		// 1000)+1));
		// }
//		System.out.println(URLPathCombine("a\\/", "\\/c"));
//		System.out.println(URLPathCombine(new String[]{"sss","ddd","fff"}));
		//System.out.println(getFileExtName("3.1"));
		 System.out.println(replaceLastBlank("aaa  sss  ddddd          ff"));

	}

	// *******************************TestMain**************************************

	/**
	 * 
	 * @Title: invertString
	 * @Description: To invert a string.e.g.:Input:"invertString Test"
	 *               Output:"tseT gnirtStrevni"
	 * @param @param initString
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String reverse(String initString) {
		// char[] initChars = initString.toCharArray();
		// char temp;
		// for (int i = 0; i < initChars.length / 2; i++) {
		// temp = initChars[i];
		// initChars[i] = initChars[initChars.length - i - 1];
		// initChars[initChars.length - i - 1] = temp;
		// }
		return new StringBuilder(initString).reverse().toString();
	}

	/**
	 * 
	 * @Title: IsNumber(integer number,float number etc.)
	 * @Description: Verify number string
	 * @param @param chkValue
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean isNumber(String chkValue) {
		String regex = "^[0-9]*[\\.]?[0-9]*$";
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(chkValue);
		if (mat.find()) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @Title: SubStrByByteLength
	 * @Description: TODO
	 * @param @param str
	 * @param @param byteLen
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String subStrByByteLength(String charsetName, String str,
			int byteLen) {
		if (isNullOrEmpty(str)) {
			return str;
		}
		int curLen = 0;
		StringBuilder ret = new StringBuilder();
		char[] chars = str.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			ret.append(chars[i]);
			curLen = ret.toString().getBytes(getEncodingCharset(charsetName)).length;
			if (curLen > byteLen) {
				ret.deleteCharAt(i);
				break;
			} else if (curLen == byteLen) {
				break;
			}
		}
		return ret.toString();
	}

	/**
	 * 
	 * @Title: isNullOrEmpty
	 * @Description: To judge whether a string is NULL or empty.
	 * @param @param str
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean isNullOrEmpty(String str) {
		return (null == str || Empty.equals(str)) ? true : false;
	}

	/**
	 * 
	 * @Title: getEncodingCharset
	 * @Description: TODO
	 * @param @param initCharsetName
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static Charset getEncodingCharset(String initCharsetName) {
		Charset charset = null;
		try {
			String temp = "";
			temp.getBytes(initCharsetName);
			charset = Charset.forName(initCharsetName);
		} catch (UnsupportedEncodingException e) {
			// TODO Set Default Charset
			charset = Charset.defaultCharset();
			// e.printStackTrace();
		}
		return charset;
	}

	/**
	 * 
	 * @Title: getStringSequence
	 * @Description: TODO
	 * @param @param startNo
	 * @param @param sequenceLength
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getStringSequence(int startNo, int sequenceLength) {
		StringBuilder sb = new StringBuilder(Integer.toString(startNo));
		while (sb.length() < sequenceLength) {
			sb.insert(0, "0");
		}
		return sb.toString();
	}

	/**
	 * 
	 * @Title: getString
	 * @Description: TODO
	 * @param @param bytes
	 * @param @param charsetName
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getString(byte[] bytes, String charsetName) {
		return new String(bytes, getEncodingCharset(charsetName));
	}

	/**
	 * 
	 * @Title: getString
	 * @Description: TODO
	 * @param @param bytes
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getString(byte[] bytes) {
		return getString(bytes, EConstants.ENCODING_NAME_ISO_8859_1);
	}

	/**
	 * 
	 * @Title: getBytes
	 * @Description: TODO
	 * @param @param str
	 * @param @param charsetName
	 * @param @return
	 * @return byte[]
	 * @throws
	 */
	public static byte[] getBytes(String str, String charsetName) {
		return str.getBytes(getEncodingCharset(charsetName));
	}

	/**
	 * 
	 * @Title: getBytes
	 * @Description: TODO
	 * @param @param str
	 * @param @return
	 * @return byte[]
	 * @throws
	 */
	public static byte[] getBytes(String str) {
		return getBytes(str, EConstants.ENCODING_NAME_ISO_8859_1);
	}

	/**
	 * 
	 * @Title: URLCombine
	 * @Description: Combine URL Path
	 * @param @param prePath
	 * @param @param nxtPath
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String URLPathCombine(String prePath, String nxtPath) {
		String finalPath = StringUtils.Empty;
		prePath = prePath.replaceAll("\\\\", "/");
		nxtPath = nxtPath.replaceAll("\\\\", "/");
		if (prePath.endsWith("/")) {
			if (nxtPath.startsWith("/")) {
				finalPath = prePath.substring(0, prePath.length() - 1)
						+ nxtPath;
			} else {
				finalPath = prePath + nxtPath;
			}
		} else {
			if (nxtPath.startsWith("/")) {
				finalPath = prePath + nxtPath;
			} else {
				finalPath = prePath + "/" + nxtPath;
			}
		}
		return finalPath;
	}

	/**
	 * 
	 * @Title: URLPathCombine
	 * @Description: Multiply URL Path Combine
	 * @param @param paths
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String URLPathCombine(String[] paths) {
		String finalPath = Empty;
		if (paths.length > 0) {
			finalPath = paths[0];
		}
		for (int i = 1; i < paths.length; i++) {
			finalPath = URLPathCombine(finalPath, paths[i]);
		}
		return finalPath;
	}
	
	/**
	 * 
	 * @Title: getFileExtensionName
	 * @Description: TODO
	 * @param @param fileName
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getFileExtName(String fileName){
		String regex = "^.+(\\.[^\\.]+)$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(fileName);
		m.find();
		try{
			return m.group(1);
		}catch(IllegalStateException e){
			return Empty;
		}
	}
	
	/**
	 * 
	 * @Title: replaceLastBlank
	 * @Description: TODO
	 * @param @param s
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String replaceLastBlank(String s){
		String regex = "^[ ]$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(s);
		m.find();
		try{
			return m.group(1);
		}catch(IllegalStateException e){
			System.out.println("Error");
			return Empty;
		}
//		s=s.replace("/([   ]{4,})/g"," ");
//		return s;
	}
}
